﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BadJokeProject.Web;
using BadJokeProject.Web.Model;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.Generic;
using Microsoft.Practices.Prism.Commands;
using System.ServiceModel.DomainServices.Client;

namespace BadJokeProject
{
    public class ViewModel : NotificationObject
    {
        private BadJokeDomainContext context = new BadJokeDomainContext();

        private DelegateCommand _loadAllBadJokesCommand = null;

        public ViewModel()
        {
            _loadAllBadJokesCommand = new DelegateCommand(() => this.LoadAllBadJokes());
        }

        private List<BadJokeItem> _itemSource;

        public List<BadJokeItem> ItemsSource
        {
            get { return _itemSource; }
            set
            {
                _itemSource = value;
                this.RaisePropertyChanged<List<BadJokeItem>>(() => this.ItemsSource);
            }
        }

        private BadJokeItem _selectedItem = null;

        public BadJokeItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                this.RaisePropertyChanged(() => this.SelectedItem);
            }
        }


        public DelegateCommand LoadAllBadJokesCommand
        {
            get { return _loadAllBadJokesCommand; }
        }

        private void LoadAllBadJokes()
        {
            LoadOperation<BadJokeItem> load = this.context.Load(this.context.GetBadJokeItemsQuery());
            load.Completed += (sender, args) =>
                {
                    this.ItemsSource = load.Entities.ToList();
                };
        }

    }
}
